<template>
  <view class="container">
    <view class="search-bar">
      <UniSearchBar :placeholder="`${$t('allocatedetail.search.placeholder')}`" v-model="keyword"
                    @confirm="search" :foucs="numFocus" @blur="formblur" @input="forminput" @cancel="cancel"
                    cancel-text="cancel">
      </UniSearchBar>
    </view>
    <view class="list">
      <uni-list>
        <uni-list-item
            v-for="(item, index) in table"
            :key="index"
            :disabled="(item.num - item.confirmNum - item.pendingNum) === 0"
            showArrow
            :title="
					`${$t('allocatedetail.item.name')} ：${item.drugName}
						${$t('allocatedetail.item.batchNumber')} ：${item.batchNumber}
						`" :note="`${getNumText}:${item.num}; ${getConfirmNumText}:${item.confirmNum}; ${$t('allocatedetail.item.pendingNum')}:${item.pendingNum};`" :rightText="`${$t('allocatedetail.item.warehouse')}:${item.warehouseName} \n ${$t('allocatedetail.item.shelfName')}:${item.shelfName}
						 `
					" link="navigateTo"
            :to="(item.num - item.confirmNum - item.pendingNum) !== 0? `/pages/storeAllocateAct/index?id=${item.id}&tabCurrent=${tabCurrent}&allocateOrderType=${allocateOrderType}` : ''" />
      </uni-list>

      <uni-load-more :status="status" :icon-size="14" :content-text="contentText" @clickLoadMore="loadmore" />
    </view>
  </view>
</template>

<script>
import api from '../../common/api.js'
import UniSearchBar from "@/components/uni-search-bar/uni-search-bar.vue";
import KeyboardListener from "uniapp-keyboard-listener";

export default {
  components: {
    UniSearchBar,
    KeyboardListener
  },
  data() {
    return {
      status: 'more',
      contentText: {
        contentdown: this.$t('load.more.contentdown'),     //'查看更多',
        contentrefresh: this.$t('load.more.contentrefresh'),  //'加载中',
        contentnomore: this.$t('load.more.contentnomore')   // '没有更多了'
      },
      id: '',
      keyword: '', //批次号
      table: [],
      pageIndex: 1,
      code:'',
      numFocus:false,
      pdacode:'',
      // tab
      tabCurrent: '',
      allocateOrderType: undefined
    }
  },
  async onUnload() {

  },
  onNavigationBarButtonTap(e) {
    console.log(e, '----e---')
    let that = this
    uni.scanCode({
      success: async function (res) {
        console.log('条码类型：' + res.scanType);
        console.log('条码内容：' + res.result);
        that.code = res.result
        await that.findByCode()
      }
    });
  },
  async onLoad(option) {
    this.id = option.id
    this.tabCurrent = option.tabCurrent
    this.allocateOrderType = option.allocateOrderType
    console.log('上一个页面传递过来的参数', option)
    if (this.id !== undefined) {
      await this.findbyId()
    } else {
      uni.navigateBack()
    }
  },
  computed: {
    // 请求函数
    pageFunc() {
      return this.tabCurrent === 'in'? api.getAllocateInboundDetailPage : api.getAllocateOutboundDetailPage
    },
    // 查询参数
    pageOrderId() {
      return this.tabCurrent === 'in'? 'inboundOrderId' : 'outboundOrderId'
    },
    // 数量
    getNumText() {
      return this.tabCurrent === 'in'? this.$t('allocatedetail.item.inboundNum') : this.$t('allocatedetail.item.outboundNum')
    },
    getConfirmNumText() {
      return this.tabCurrent === 'in'? this.$t('allocatedetail.item.inboundConfirmNum') : this.$t('allocatedetail.item.outboundConfirmNum')
    },
  },
  methods: {
    forminput(){
      this.pdacode = ''
    },
    formfocus() {
      this.pdacode = ''
      setTimeout(() => {
        this.isFocus = false
        this.numFocus = true
      }, 0)
    },
    formblur() {
      // console.log(e.detail.value)
      this.pdacode = ''
      this.numFocus = false;
    },
    async scancodelisten(e) {
      let keyCode = e.keyCode
      // console.log(e, '-----e---')
      if ((keyCode >= 65 && keyCode <= 90) || (keyCode >= 97 && keyCode <= 122)) {
        console.log('字母输入:', e.key);
        this.pdacode = this.pdacode + e.key
      } else if (keyCode >= 48 && keyCode <= 57) {
        console.log('数字输入:', e.key);
        console.log(this.numFocus, 'numFocus');
        this.pdacode = this.pdacode + e.key
      } else if (keyCode === 13) {
        this.code = this.pdacode
        if(this.code!==''){
          await this.findByCode()
        }
        this.pdacode = ''
      } else {
        console.log('其他输入:', e.key);
      }

      console.log(this.pdacode, '-----this.pdacode---')
    },
    async findByCode() {
      uni.showLoading()
      let ret = await this.pageFunc({
        id: this.code,
        page: { pageNumber: 1, pageSize: 50 },
      })
      if (ret.code === 0 && ret.data) {
        if (ret.data[this.pageOrderId] === this.id) {
          uni.navigateTo({
            url: `/pages/storeAllocateAct/index?id=${this.code}&tabCurrent=${this.tabCurrent}&allocateOrderType=${this.allocateOrderType}`
          })
        } else {
          this.$nextTick(() => {
            uni.showToast({
              title: this.$t('scanfun.fail'),
              icon: 'none'
            });
          })
        }
      } else {
        uni.showToast({
          title: ret.message,
          icon: 'none'
        });
      }
      uni.hideLoading()
    },
    async findbyId() {
      let ret = await this.pageFunc({
        keyword: "",
        page: { pageNumber: 1, pageSize: 50 },
        [this.pageOrderId]: this.id,
      })
      if (ret.code === 0) {
        this.table = ret.data.items
      }
    },
    async cancel() {
      this.pageIndex = 1
      this.keyword = ''
      await this.findbyId();
    },
    async search(e) {
      this.pageIndex = 1
      this.keyword = e.value
      let ret = await this.pageFunc({
        keyword: this.keyword,
        page: { pageNumber: 1, pageSize: 50 },
        [this.pageOrderId]: this.id,
      })
      if (ret.code === 0) {
        this.table = ret.data.items
      } else {
        this.table = []
      }
    },
    async loadmore() {
      this.pageIndex++
      let ret = await this.pageFunc({
        keyword: this.keyword,
        page: { pageNumber: this.pageIndex, pageSize: 50 },
        [this.pageOrderId]: this.id,
      })
      if (ret.code === 0) {
        if (ret.data.items.length > 0) {
          this.table = [].concat(this.table, ret.data.items)
        } else {
          this.pageIndex--
        }
      } else {
        this.pageIndex--
      }
    }
  }
}
</script>

<style>
.container {
  height: 100vh;
}

.search-bar {
  padding: 10px;
  /* 根据实际高度调整，示例假设搜索栏高度约50px */
  flex-shrink: 0;
  margin-left: 10rpx;
  margin-right: 20rpx;
}

.title-item {

  padding-bottom: 10rpx;
  padding-top: 10rpx;
}
</style>